{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('12.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签节点</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a2\">a2</a>\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "div = sp.div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "span = \"<span class'sp'>1234567890</span>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(span)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html><body><span class=\"\">1234567890</span></body></html>\n"
     ]
    }
   ],
   "source": [
    "## 自动补全html、body 标签\n",
    "span_tag1 = BeautifulSoup(span, 'lxml')\n",
    "print(span_tag1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html><head></head><body><span class'sp'=\"\">1234567890</span></body></html>\n"
     ]
    }
   ],
   "source": [
    "## 自动补全html、head、body标签\n",
    "span_tag2 = BeautifulSoup(span, 'html5lib')\n",
    "print(span_tag2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<span class'sp'=\"\">1234567890</span>\n"
     ]
    }
   ],
   "source": [
    "## 按照原有格式转换\n",
    "span_tag3 = BeautifulSoup(span, 'html.parser')\n",
    "print(span_tag3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div id=\"div1\">\n",
      "<p id=\"p\">p1</p>\n",
      "<a class=\"ac\" href=\"a1\">a1</a>\n",
      "<html><body><span class=\"\">1234567890</span></body></html></div>\n"
     ]
    }
   ],
   "source": [
    "div.insert(100, span_tag1)\n",
    "print(div)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div id=\"div1\">\n",
      "<p id=\"p\">p1</p>\n",
      "<a class=\"ac\" href=\"a1\">a1</a>\n",
      "<html><body><span class=\"\">1234567890</span></body></html><html><head></head><body><span class'sp'=\"\">1234567890</span></body></html></div>\n"
     ]
    }
   ],
   "source": [
    "div.insert(100, span_tag2)\n",
    "print(div)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div id=\"div1\">\n",
      "<p id=\"p\">p1</p>\n",
      "<a class=\"ac\" href=\"a1\">a1</a>\n",
      "<html><body><span class=\"\">1234567890</span></body></html><html><head></head><body><span class'sp'=\"\">1234567890</span></body></html><span class'sp'=\"\">1234567890</span></div>\n"
     ]
    }
   ],
   "source": [
    "div.insert(100, span_tag3)\n",
    "print(div)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
